package com.coustemview.qianfeng.huaqiangbeistore.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.model.GoodsBean;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by huaxin on 2016/8/30.
 */
public class MySpecialofferAdapter extends BaseAdapter {
    private Context mContext;
    // private List<SpecialOfferBean.DataBean> mData;
    private List<GoodsBean> mData;
    private List<Integer> list = new ArrayList<>();
    private List<Integer> listTwo = new ArrayList<>();
    private ImageOptions mOptions;

    public MySpecialofferAdapter(Context context, List<GoodsBean> data) {
        mContext = context;
        mData = data;
        mOptions = new ImageOptions.Builder().setUseMemCache(true).build();
        Log.d("tag", "MySpecialofferAdapter: " + mData);
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        long currentTime = System.currentTimeMillis();
        long buyStartTime = mData.get(position).buyStartTime;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String format = sf.format(buyStartTime);
        String start = sdf.format(buyStartTime);
        String current = sdf.format(currentTime);
        if (mData.get(position).buyStartTime <= currentTime) {
            convertView = null;
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_specialoffer, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (start.equals(current)) {
                if (position == 0) {
                    holder.mTextViewBigTitle.setVisibility(View.VISIBLE);
                } else {
                    holder.mTextViewBigTitle.setVisibility(View.INVISIBLE);
                }
            } else if (!start.equals(current)) {
                list.add(position);
                if (position == list.get(0)) {
                    holder.mTextViewBigTitle.setVisibility(View.VISIBLE);
                    holder.mTextViewBigTitle.setText("最后疯抢");
                } else {
                    holder.mTextViewBigTitle.setVisibility(View.INVISIBLE);
                }
            }

            long buyEndTime = mData.get(position).buyEndTime;
            long l = (buyEndTime - currentTime) / 1000 / 3600 / 24;
            holder.mTextViewDay.setText("剩" + l + "天");
            holder.mTextViewHotSpot.setText(mData.get(position).hotSpot);
            holder.mTextViewTitle.setText(mData.get(position).title);
            holder.mTextViewPrice.setText("￥" + mData.get(position).price);
            holder.mTextViewBuyingPrice.setText("" + mData.get(position).panicBuyingPrice);
            x.image().bind(holder.mImageViewImageMD5, mData.get(position).imageMd5, mOptions);
            return convertView;
        } else {
            convertView = null;
            ViewHolderTwo holderTwo = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_specialoffer_two, null);
                holderTwo = new ViewHolderTwo(convertView);
                convertView.setTag(holderTwo);
            } else {
                holderTwo = (ViewHolderTwo) convertView.getTag();
            }
            listTwo.add(position);
            if (position == listTwo.get(0)) {
//                return i2;
//            }
                holderTwo.mTextViewBigTitleTwo.setVisibility(View.VISIBLE);
            } else {
                holderTwo.mTextViewBigTitleTwo.setVisibility(View.INVISIBLE);
            }
            x.image().bind(holderTwo.mImageViewMD5Two, mData.get(position).imageMd5, mOptions);
            holderTwo.mTextViewTitleTwo.setText(mData.get(position).title);
            String[] split = format.split("-");
            holderTwo.mTextViewDayTwo.setText(split[1] + "月" + split[2] + "日" + split[3] + "点" + "开售");
            return convertView;
        }
    }

    public static class ViewHolder {
        private TextView mTextViewBigTitle, mTextViewHotSpot, mTextViewDay,
                mTextViewTitle, mTextViewBuyingPrice, mTextViewPrice;
        private ImageView mImageViewImageMD5;

        public ViewHolder(View view) {
            mTextViewBigTitle = (TextView) view.findViewById(R.id.tv_item_specialoffer_big_title);
            mTextViewHotSpot = (TextView) view.findViewById(R.id.tv_item_specialoffer_hotSpot);
            mTextViewDay = (TextView) view.findViewById(R.id.tv_item_specialoffer_day);
            mTextViewTitle = (TextView) view.findViewById(R.id.tv_item_specialoffer_title);
            mTextViewBuyingPrice = (TextView) view.findViewById(R.id.tv_item_specialoffer_panicBuyingPrice);
            mTextViewPrice = (TextView) view.findViewById(R.id.tv_item_specialoffer_price);
            mImageViewImageMD5 = (ImageView) view.findViewById(R.id.iv_item_specialoffer_imageMd5);
        }
    }

    public static class ViewHolderTwo {
        private TextView mTextViewBigTitleTwo, mTextViewDayTwo, mTextViewTitleTwo;
        private ImageView mImageViewMD5Two;

        public ViewHolderTwo(View view) {
            mTextViewBigTitleTwo = (TextView) view.findViewById(R.id.tv_item_specialoffer_big_title_two);
            mTextViewDayTwo = (TextView) view.findViewById(R.id.tv_item_specialoffer_day_two);
            mTextViewTitleTwo = (TextView) view.findViewById(R.id.tv_item_specialoffer_title_two);
            mImageViewMD5Two = (ImageView) view.findViewById(R.id.iv_item_specialoffer_imageMd5_two);
        }
    }

    //    @Override
//    public int getViewTypeCount() {
//        return 2;
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        return position / (list.get(list.size() - 1) + 1);
//    }
}
